import 'package:finance_app/generated/json/base/json_convert_content.dart';
import 'package:finance_app/generated/json/base/json_field.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';

class ChatRoomMessageEntity with JsonConvert<ChatRoomMessageEntity> {
  @JSONField(name: "data_type")
  String dataType;
  int id;
  @JSONField(name: "chat_room_id")
  int chatRoomId;
  @JSONField(name: "message_id")
  int messageId;
  @JSONField(name: "user_id")
  dynamic userId;
  String type;
  String content;
  dynamic actions;
  String avatar;
  dynamic name;
  @JSONField(name: "created_at")
  String createdAt;
  @JSONField(name: "updated_at")
  String updatedAt;
}

class ChatRoomMessageGroupEntity {
  String avatar;
  dynamic userId;
  dynamic name;
  String createdAt;
  List<ChatRoomMessageEntity> messages;

  static List<ChatRoomMessageGroupEntity> addMessage(
      List<ChatRoomMessageGroupEntity> groups,
      List<ChatRoomMessageEntity> messages) {
    if (groups == null || groups.length == 0) return formatMessage(messages);
    List<ChatRoomMessageGroupEntity> result = groups;
    ChatRoomMessageGroupEntity groupEntity = groups.removeLast();
    for (final item in messages) {
      if (groupEntity == null) {
        groupEntity = ChatRoomMessageGroupEntity()
          ..createdAt = item.createdAt
          ..userId = item.userId
          ..messages = [item]
          ..avatar = item.avatar
          ..name = item.name;
      } else {
        DateTime msgTime =
            DateTimeUtils.toDateTime(item.createdAt, 'yyyy-MM-dd HH:mm:ss');
        DateTime preTime = DateTimeUtils.toDateTime(
            groupEntity.createdAt, 'yyyy-MM-dd HH:mm:ss');
        if (item.userId == groupEntity.userId &&
            groupEntity?.createdAt != null &&
            msgTime.difference(preTime).inMinutes < 2) {
          groupEntity.messages.add(item);
        } else {
          result.add(groupEntity);
          groupEntity = ChatRoomMessageGroupEntity()
            ..createdAt = item.createdAt
            ..userId = item.userId
            ..messages = [item]
            ..avatar = item.avatar
            ..name = item.name;
        }
      }
    }
    if (groupEntity != null) {
      result.add(groupEntity);
    }
    return result;
  }

  static List<ChatRoomMessageGroupEntity> formatMessage(
      List<ChatRoomMessageEntity> messages) {
    List<ChatRoomMessageGroupEntity> result = [];
    ChatRoomMessageGroupEntity groupEntity;
    for (final item in messages) {
      if (groupEntity == null) {
        groupEntity = ChatRoomMessageGroupEntity()
          ..createdAt = item.createdAt
          ..userId = item.userId
          ..messages = [item]
          ..avatar = item.avatar
          ..name = item.name;
      } else {
        DateTime msgTime =
            DateTimeUtils.toDateTime(item.createdAt, 'yyyy-MM-dd HH:mm:ss');
        DateTime preTime = DateTimeUtils.toDateTime(
            groupEntity.createdAt, 'yyyy-MM-dd HH:mm:ss');
        if (item.userId == groupEntity.userId &&
            groupEntity?.createdAt != null &&
            msgTime.difference(preTime).inMinutes < 2) {
          groupEntity.messages.add(item);
        } else {
          result.add(groupEntity);
          groupEntity = ChatRoomMessageGroupEntity()
            ..createdAt = item.createdAt
            ..userId = item.userId
            ..messages = [item]
            ..avatar = item.avatar
            ..name = item.name;
        }
      }
    }
    if (groupEntity != null) {
      result.add(groupEntity);
    }
    return result;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is ChatRoomMessageGroupEntity &&
          runtimeType == other.runtimeType &&
          avatar == other.avatar &&
          userId == other.userId &&
          name == other.name &&
          createdAt == other.createdAt &&
          messages == other.messages;

  @override
  int get hashCode =>
      avatar.hashCode ^
      userId.hashCode ^
      name.hashCode ^
      createdAt.hashCode ^
      messages.hashCode;
}
